<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sale_ctrl extends CI_Controller {

	function __construct()
	{
		parent::__construct ();
		$this->load->model('usertype_model');
		$this->load->model('users_model');
		$this->load->model('drugs_model');
		$this->load->model('order_model');
		header("Content-type: text/html; charset=utf-8");
		
	}
	
	function index() {
	
		$session_data = $this->session->userdata('logged_in');
		$usertype_id = $session_data["usertype_id"];
		$data['user'] = $this->users_model->select_profile($usertype_id);
		
		$data['drugs'] = $this->drugs_model->_getAll();
		$data['result'] = $this->drugs_model->select_all();
		
		$this->load->view('adminsite/sale_view',$data);
	}
	
	function sale_user() {
		
		$session_data = $this->session->userdata('logged_in');
		$usertype_id = $session_data["usertype_id"];
		$data['user'] = $this->users_model->select_profile($usertype_id);
		
		$data['drugs'] = $this->drugs_model->_getAll();
		$data['result'] = $this->drugs_model->select_all();
		
		$this->load->view('usersite/sale_view',$data);
	}
	
	public function buy_cart() {
	
		$mdc_id = $this->uri->segment('3');
		
		$this->session->set_userdata('num', '7');
		
		$num = $this->session->userdata('num');
		//echo $num;
		//echo "test".'<br>';
	
		$amount = $this->input->post('amount');
		$price = $this->input->post('price');
		$total = $amount*$price;
		
		
		$mdc = $this->drugs_model->select_mdc_id($mdc_id);

		$user_id = $this->session->userdata('logged_in');
		//echo 'user_id : '.$user_id['user_id'];
		
		if($this->session->userdata('order_id')){
				
			$order_id = $this->session->userdata('order_id');
			 $total = $this->order_model->select_total($order_id);

			 $sum = $total['total'];
			
			 $total['total'] = ($amount*$price) + $sum;
			 $this->order_model->update_total($order_id,$total);
			
			 
		}else{
			//echo "not session".'<br>';
			
			$data1 = array(
			'user_id' => $user_id['user_id'],
			'total' => $total
			);
			
			$order_id = $this->order_model->insert($data1);
			
		}
		
			$data2 = array(
			'order_id'  => $order_id,
			'price'   => $price,
			'mdc_id'   => $mdc_id,
			'order_amount'  => $amount
		);
		
		
		$this->order_model->insert_order($data2);
		
		$data3['order_mdc'] = $this->order_model->select_order_mdc($order_id);
		$data3['total'] = $this->order_model->select_total($order_id);
	
		//echo 'order_id  :'.$order_id.'<br>';
		
		//$aa = $order_id;
		//$this->session->set_userdata('order_id_ses',$order_id);
		
				$this->session->set_userdata('order_id', $order_id);
				$this->session->set_userdata('num_order', '4');
		//$this->session->set_userdata('set',$order_id);
					
					
		$this->load->view('adminsite/sale_cart',$data3);
		
	}
	
	/*function cart() {
			
		$this->load->view('adminsite/sale_view');
		$this->load->view('usersite/sale_view');	
	}
	
	public function delect_cart()
	{
		 $id_mdc = $this->uri->segment(3);
		 $id = $this->session->userdata('order_id');
		 //echo $id_img;
		  $price = $this->order_model->select_price($mdc_id);
		  //print_r($price['price']);
		  
		  		 $amount = $this->cart_model->select_amount($id);
				// print_r($amount);
				 $amount_all = $amount['order_amount'];	
			 	$total['order_amount'] = $amount_all - $price['price'];
				//echo $total['order_amount'];
			 $this->cart_model->update_amount($id,$total);
			
		 $this->order_model->delete_mdc($id_img);
		 
		 $data['mdc_order'] = $this->order_model->select_order_mdc($id);
		 $data['order'] = $this->cart_model->select_order($id);
		 		
		$this->load->view('sale_cart',$data); 
	}
	
	public function show_cart() { 
		
		 $id = $this->session->userdata('order_id');				 
		 $data['mdc_order'] = $this->order_model->select_order_mdc($id);
		 $data['order'] = $this->cart_model->select_order($id);
		 	
		$this->load->view('sale_cart',$data);
	}
	
	function show_cart_confirm() {
	
		 $id = $this->session->userdata('order_id');		 			 
		 $data['mdc_order'] = $this->order_model->select_order_mdc($id);
		 $data['order'] = $this->cart_model->select_order($id);
		 		
		$this->load->view('sale_cart',$data);
	}
	
	function confirm() {
	
		 $id = $this->session->userdata('order_id');
		 $data = array(
		 	'status_confirm' => '1'
		 );
		 $this->cart_model->update_status($id,$data);
		 $this->session->unset_userdata('order_id');
		 
		 $user_id = $this->session->userdata('uid');
		 $data['order_user'] = $this->cart_model->select_order_user($user_id);
		 		
		 $this->load->view('sale_cart',$data);
	}
	*/
	
	function test()
	{
		$num = $this->session->userdata('order_id');
		echo $num;
	}
	
	function confirm_order()
	{
		$order_id = $this->session->set_userdata('order_id');
		$data = array(
			'order_confirm'  => '1'
		);
		$this->order_model->update_confirm_order($order_id,$data);
		$this->session->unset_userdata('order_id');
		$this->index();
	}
	
	function delete_mdc()
	{
		$order_id = $this->session->userdata('order_id');
		$mdc_id = $this->uri->segment(3);
		
		$data3['order_mdc'] = $this->order_model->select_order_mdc($order_id);
		//print_r($data3['order_mdc']);
		$data3['total'] = $this->order_model->select_total($order_id);
		//print_r($data3['total']);
		$total_all = $data3['total'];
		$price = $this->order_model->select_mdc_mdc_id($order_id,$mdc_id);

		$tatal_row = $price['price']*$price['order_amount'];
		//print_r($price);
		//echo $tatal_row;
		 //print_r($total_all['total']);
		 $total['total'] =  $total_all['total']-$tatal_row;
		 $totalsum = $total['total'];
		//echo $totalsum;
		$data = array(
		'total' => $totalsum
		);
		$this->order_model->update_totalsum($order_id,$data);
		
		
		$this->order_model->delete_mdc_order($mdc_id,$order_id);
		
		$this->load->view('adminsite/sale_cart',$data3);
		
	}
}